package com.zmx.doctor.view.activity;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LevelListDrawable;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.widget.TextView;

import androidx.annotation.RequiresApi;

import com.google.gson.Gson;
import com.zmx.doctor.BaseActivity;
import com.zmx.doctor.R;
import com.zmx.doctor.okhttp.OkHttp3ClientManager;
import com.zmx.doctor.pojo.ArticleMessage;
import com.zmx.doctor.utils.AllUtil;
import com.zmx.doctor.view.util.MySharedPreferences;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 程序猿：胖胖祥
 * 时 间：2020/10/17  17:36
 * 功 能：推荐详情
 */
public class RecommendedCasesDetailsActivity extends BaseActivity {


    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.content)
    TextView content;

    private String cases_id;

    private LevelListDrawable mDrawable = new LevelListDrawable();

    @Override
    protected int getLayoutId() {

        return R.layout.activity_recommended_cases_details;

    }

    @Override
    protected void initViews() {

        ButterKnife.bind(this);
        setToolbar(R.id.tool_bar);

        cases_id = this.getIntent().getStringExtra("cases_id");
        getCasesDis(cases_id);


    }
    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @RequiresApi(api = Build.VERSION_CODES.N)
        @Override
        public void handleMessage(Message msg) {

            switch (msg.what){

                case 1123:
                    Bitmap bitmap = (Bitmap)msg.obj;
                    BitmapDrawable drawable = new BitmapDrawable(null, bitmap);
                    mDrawable.addLevel(1, 1, drawable);
                    mDrawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
                    mDrawable.setLevel(1);

                    CharSequence charSequence = content.getText();
                    content.setText(charSequence);
                    content.invalidate();
                    break;

                case 1:

                    try {

                        JSONObject object = new JSONObject(msg.obj.toString());

                        JSONObject data = object.getJSONObject("data");

                        title.setText(data.getString("title"));
                        content.setText(Html.fromHtml(data.getString("content"), Html.FROM_HTML_MODE_COMPACT, new Html.ImageGetter() {
                            @Override
                            public Drawable getDrawable(final String source) {
                                new Thread(new Runnable() {
                                    @Override
                                    public void run() {
                                        mDrawable.addLevel(0, 0, getResources().getDrawable(R.mipmap.ic_launcher));
                                        mDrawable.setBounds(0, 0, 200, 200);

                                        Bitmap bitmap;
                                        try {
                                            bitmap = BitmapFactory.decodeStream(new URL(source).openStream());
                                            Message msg = handler.obtainMessage();
                                            msg.what = 1123;
                                            msg.obj = bitmap;
                                            handler.sendMessage(msg);
                                        } catch (Exception e) {
                                            e.printStackTrace();
                                        }
                                    }
                                }).start();
                                return mDrawable;
                            }
                        }, null));

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    break;

            }

        }
    };


    /**
     * 推荐
     */
    public void getCasesDis(String cases_id) {

        List<String> ls = new ArrayList<>();
        ls.add(cases_id);
        //请求网络
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sign", AllUtil.md5(sing(ls)));
        params.put("token", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.token, ""));
        params.put("uid", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        params.put("ts", System.currentTimeMillis() / 1000); // 当前时间戳
        params.put("cmd", "getCasesDis");
        params.put("data[cases_id]", cases_id);
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/doctor", params, handler, 1, 404);

    }


}
